package com.fulihui.information.core.redis;

import com.fulihui.information.AbstractToStringSupport;
import com.fulihui.information.biz.conv.ContentConv;
import com.fulihui.information.dal.dataobj.InfoContent;
import com.fulihui.information.dal.dataobj.InfoContentExample;
import com.fulihui.information.dal.mapper.ExtInfoContentListItemMapper;
import com.fulihui.information.dto.ContentListItemDTO;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import java.util.*;

import static com.fulihui.information.util.ContentScoreUtil.*;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * @author Willard Hu on 2017/1/12.
 */
@ContextConfiguration(locations = {"classpath:spring/test-init-redis-sort-context.xml"})
public class InitRedisSortData extends AbstractToStringSupport {
    @Autowired
    private ExtInfoContentListItemMapper      extInfoContentListItemMapper;
    @Autowired
    private RedisBasicCategoryContentSortDB   redisBasicCategoryContentSortDB;
    @Autowired
    private RedisHotsContentReadTimesSortDB   redisHotsContentReadTimesSortDB;
    @Autowired
    private RedisHotsContentSharedTimesSortDB redisHotsContentSharedTimesSortDB;
    @Autowired
    private RedisContentListItemDB            redisContentListItemDB;

    private static final List<String> CATEGORY_CODES =
            Arrays.asList("YOUKANTOU_ESSAY_JOKE", "YOUKANTOU_IMAGES_ESSAY", "YOUKANTOU_IMAGES_FUNNY",
            "YOUKANTOU_NEWS_EMOTION", "YOUKANTOU_NEWS_ENTERTAINMENT", "YOUKANTOU_NEWS_SOCIETY");
            //Collections.singletonList("YOUKANTOU_IMAGES_ESSAY");

    /**
     * 类目代码：
     * YOUKANTOU_ALL_HOTS
     * <p>
     * YOUKANTOU_ESSAY_JOKE
     * YOUKANTOU_IMAGES_ESSAY
     * YOUKANTOU_IMAGES_FUNNY
     * YOUKANTOU_NEWS_EMOTION
     * YOUKANTOU_NEWS_ENTERTAINMENT
     * YOUKANTOU_NEWS_SOCIETY
     */
    @Test
    public void init_data() {
        CATEGORY_CODES.forEach(it -> {
            InfoContentExample example = new InfoContentExample();
            example.createCriteria().andCategoryCodeEqualTo(it)
                    .andTopStatusEqualTo("0")
                    .andStatusEqualTo("CHECK");
            int start = 0, rows = 500;
            for (; ; ) {

                List<InfoContent> list = extInfoContentListItemMapper.selectListItemPage(example, start, rows);
                if (isEmpty(list)) {
                    break;
                }
                List<ContentListItemDTO> items = ContentConv.toList(list, ContentListItemDTO.class);
                redisContentListItemDB.batchSave(items);
                Map<String, Double> params = new HashMap<>();
                items.forEach(item -> params.put(item.getId(), categoryScore(item)));
                redisBasicCategoryContentSortDB.bathSave(params, it);
                // 段子不加入到热门中
                if (!"YOUKANTOU_ESSAY_JOKE".equals(it)) {
                    Map<String, Double> hotsParams = new HashMap<>();
                    items.forEach(item -> hotsParams.put(item.getId(), hotsReadTimesScore(item)));
                    redisHotsContentReadTimesSortDB.bathSave(hotsParams, null);

                    Map<String, Double> hotsParams2 = new HashMap<>();
                    items.forEach(item -> hotsParams2.put(item.getId(), hotsSharedTimesScore(item)));
                    redisHotsContentSharedTimesSortDB.bathSave(hotsParams2, null);
                }
                start = start + rows + 1;
            }
        });
    }

}
